/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.actions;

import java.util.List;

import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.TreeViewer;

import edu.pku.sei.modeler.actions.DiagramCreateAction;
import edu.pku.sei.modeler.actions.OpenDiagramAction;
import edu.pku.sei.modeler.actions.registry.EditorActionGroup;
import edu.pku.sei.modeler.actions.registry.IActionProvider;
import edu.pku.sei.modeler.actions.registry.TreeViewActionGroup;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.transformation.editor.binding.IDiagramBindingElement;
import edu.pku.sei.transformation.editor.controller.editparts.node.CommentEditpart;
import edu.pku.sei.transformation.editor.controller.editparts.node.ConstraintEditpart;
import edu.pku.sei.transformation.editor.controller.editparts.node.MetaModelEditpart;
import edu.pku.sei.transformation.editor.controller.editparts.node.TransformationGoalEditpart;
import edu.pku.sei.transformation.editor.internal.TransformationEditorModelRoot;
import edu.pku.sei.transformation.editor.internal.TransformationEditorPaletteFactory;
import edu.pku.sei.transformation.editor.internal.TransformationEditorProject;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.Comment;
import edu.pku.sei.transformation.editor.model.MetaModel;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.Transformation;
import edu.pku.sei.transformation.editor.model.constraint.Constraint;
import edu.pku.sei.transformation.editor.model.requirement.Description;
import edu.pku.sei.transformation.editor.model.requirement.TransformationGoal;

/**
 * @generated
 * 
 */
public class TransformationEditorActionProvider implements IActionProvider {
	public TransformationEditorActionProvider() {

	}

	public Action getTreeViewerDoubleClickAction(Object treeObj) {
		if (treeObj instanceof MetaDiagram) {
			OpenDiagramAction opa = new OpenDiagramAction();
			opa.setTreeObj(treeObj);
			return opa;
		} else if (treeObj instanceof IDiagramBindingElement) {
			OpenBindedDiagram opa = new OpenBindedDiagram(
					(IDiagramBindingElement) treeObj);
			return opa;
		}
		return null;
	}

	@Override
	public String getProjectNature() {
		return TransformationEditorProject.PROJECT_NATURE;
	}

	@Override
	public TreeViewActionGroup getTreeViewerMenuActionGroup(TreeViewer tv) {
		return new QvtEditorTreeViewActionGroup(tv);
	}

	private class QvtEditorTreeViewActionGroup extends TreeViewActionGroup {

		public QvtEditorTreeViewActionGroup(TreeViewer tv) {
			super(tv);
		}

		@Override
		public void fillContextMenu(IMenuManager menu) {
			List selectedElement = super.getSelection();
			if (selectedElement.size() < 1)
				return;
			Object treeObj = selectedElement.get(0);
			if (treeObj instanceof MetaPackage) {
				

				// dAction = new DiagramCreateAction("Transformation");
				// dAction.setTreeObj(treeObj);
				// menu.add(dAction);
				//
				// dAction = new DiagramCreateAction("Relation");
				// dAction.setTreeObj(treeObj);
				// menu.add(dAction);
				if (((MetaPackage) treeObj).getName().equals(
						TransformationEditorModelRoot.TRANSFORMATION_PACKAGE)) {
//					CreateTransformationAction cAction = new CreateTransformationAction(
//							(MetaPackage) treeObj);
//					menu.add(cAction);
					
					DiagramCreateAction daction = new DiagramCreateAction(TransformationEditorPaletteFactory.TRANSFORMATION_STRUCTURE_DIAGRAM);
					daction.setTreeObj(treeObj);
					menu.add(daction);
				} 
				else if (((MetaPackage) treeObj).getName().equals(
						TransformationEditorModelRoot.EXECUTION_PACKAGE)) {
					DiagramCreateAction dAction;
					dAction = new DiagramCreateAction(TransformationEditorPaletteFactory.TRANSFORMATION_EXECUTION);
					dAction.setTreeObj(treeObj);
					menu.add(dAction);
				} else if (((MetaPackage) treeObj).getName().equals(
						TransformationEditorModelRoot.METAMODEL_PACKAGE)) {
//					LoadMetaModelAction action = new LoadMetaModelAction(
//							(MetaPackage) treeObj);
//					menu.add(action);
				} else if (((MetaPackage)treeObj).getName().equals(TransformationEditorModelRoot.REQUIREMENT_PACKAGE)) {
					DiagramCreateAction dAction = new DiagramCreateAction(TransformationEditorPaletteFactory.TRANSFORMATION_REQUIREMENT);
					dAction.setTreeObj(treeObj);
					menu.add(dAction);
				} else if (((MetaPackage)treeObj).getName().equals(TransformationEditorModelRoot.TEST_PACKAGE)) {
					DiagramCreateAction dAction = new DiagramCreateAction(TransformationEditorPaletteFactory.TRANSFORMATION_TEST);
					dAction.setTreeObj(treeObj);
					menu.add(dAction);
				}

			} else if (treeObj instanceof Transformation) {
				OpenBindedDiagram openAction = new OpenBindedDiagram(
						(Transformation) treeObj);
				menu.add(openAction);

				if (treeObj instanceof AtomicTransformation) {
					ExportXMLAction eaction = new ExportXMLAction(
							(AtomicTransformation) treeObj);
					menu.add(eaction);
				}	
			} else if (treeObj instanceof Rule) {
				OpenBindedDiagram openAction = new OpenBindedDiagram(
						(IDiagramBindingElement) treeObj);
				menu.add(openAction);
			}

		}

	}

	@Override
	public EditorActionGroup getEditorActionGroup(EditPartViewer viewer,
			ActionRegistry defaultActions, MetaDiagram diagram) {
		return new QvtEditorContextMenuGroup(viewer, defaultActions, diagram);
	}

	private class QvtEditorContextMenuGroup extends EditorActionGroup {

		public QvtEditorContextMenuGroup(EditPartViewer viewer,
				ActionRegistry defaultActions, MetaDiagram diagram) {
			super(viewer, defaultActions, diagram);
		}

		@Override
		public void fillContextMenu(IMenuManager menu) {
			this.fillDefaultActions(menu);

			if (getViewer().getSelectedEditParts().size() > 0) {
				if (getViewer().getSelectedEditParts().get(0) instanceof CommentEditpart) {
					MetaNode node = (MetaNode) ((CommentEditpart) getViewer()
							.getSelectedEditParts().get(0)).getModel();
					OpenContentDialogAction a = new OpenContentDialogAction(
							(Comment) node.getModelElement());
					menu.add(a);
				} else if (getViewer().getSelectedEditParts().get(0) instanceof MetaModelEditpart) {			
					MetaNode node = (MetaNode) ((MetaModelEditpart)getViewer().getSelectedEditParts().get(0)).getModel();
					LoadMetaModelAction action = new LoadMetaModelAction((MetaModel) node.getModelElement(), node.getModel());
					menu.add(action);
				} else if (getViewer().getSelectedEditParts().get(0) instanceof TransformationGoalEditpart) {
					
					IMenuManager submenu = new MenuManager("Transformation Goal");
					MetaNode node = (MetaNode) ((TransformationGoalEditpart)getViewer().getSelectedEditParts().get(0)).getModel();
					
					TransformationGoal goal = (TransformationGoal) node.getModelElement();
					OpenDescriptionAction goalDesAction = new OpenDescriptionAction(goal.getGoalDescription(), true);
					
					submenu.add(new Separator("GOAL_DES"));
					submenu.appendToGroup("GOAL_DES", goalDesAction);
					
					submenu.add(new Separator("END_DES"));
					for (Object des : goal.getEndDescription()) {
						OpenDescriptionAction endAction = new OpenDescriptionAction((Description)des);
						submenu.appendToGroup("END_DES", endAction);
					}
					CreateDescriptionAction cAction = new CreateDescriptionAction(goal);
					submenu.appendToGroup("END_DES", cAction);
					menu.add(submenu);	
				} else if (getViewer().getSelectedEditParts().get(0) instanceof ConstraintEditpart) {
					MetaNode node = (MetaNode) ((ConstraintEditpart)getViewer().getSelectedEditParts().get(0)).getModel();
					OpenConstraintDialogAction action = new OpenConstraintDialogAction((Constraint)node.getModelElement());
					menu.add(action);
				}
			}
		}
	}
}
